<!-- 正文开始 -->
<div class="layui-fluid" id="bedSearch">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="bedSearch">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-label">科室</label>
                        <div class="layui-inline item">
                            <select name="deptCode" lay-filter="bedSearch-deptCode" >
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button class="layui-btn icon-btn btnAdd"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button class="layui-btn icon-btn btnSave layui-btn-normal"><i class="layui-icon layui-icon-ok"></i>保存</button>
                    </div>
                </div>
            </div>
            <div class="bedTableWrapper">
                <table class="layui-table" id="bedTable" lay-filter="bedTable"></table>
            </div>

        </div>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="tpl-bedTable-operator">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="tpl-bedTable-status">
    <input type="checkbox" lay-filter="tpl-bedTable-status" value="{{d.id}}" lay-skin="switch" lay-text="使用|空闲"
           {{d.status==1 ? 'checked' : ''}}/>
</script>


<script>
    layui.use(['layer', 'table', 'form','http', 'tools'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var $view = $("#bedSearch");
        var $tbview = $view.find(".bedTableWrapper");
        var $deptCode = $view.find("select[name='deptCode']");
        var orderByClause = "roomCode,bedCode";

        tools.select.deptCodeOptions($deptCode);
        form.render('select','bedSearch');

        var fnGetSearchParam = function () {
            return {
                orderByClause: orderByClause,
                deptCode: $deptCode.val()
            }
        };
        //渲染表格
        var bedTableIns = table.render({
            id: 'bedTable',
            elem: '#bedTable',
            method: 'post',
            contentType: "application/json",
            url: http.base_server + 'bed/list',
            where: fnGetSearchParam(),
            page: true,
            cellMinWidth: 100,
            height: 'full-200',
            limits : [10,20,30,40,50,100],
            limit : 20,
            autoSort: false,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            cols: [[
                {type: 'numbers'},
                {type:'checkbox'},
                {field: 'roomCode', sort: true, title: '房间号',width:90, edit: 'text'},
                {field: 'bedCode', sort: true, title: '床号',width:90, edit: 'text'},
                {field: 'bedName', sort: true, title: '床名',width:90, edit: 'text'},
                {field: 'bedName2', sort: true, title: '床名2',width:90, edit: 'text'},
                {field: 'barcode', sort: true, title: '条码',width:90, edit: 'text'},
                {field: 'bedCardNo', sort: true, title: '床头卡号',width:180, edit: 'text'},
                {field: 'remark', sort: true, title: '备注',width:200, edit: 'text'},
                {field: 'status', sort: true, templet: '#tpl-bedTable-status', title: '状态',width:70, align:"center"},
                {align: 'center', toolbar: '#tpl-bedTable-operator', title: '操作', width: 80},
                {field: 'updateTime', sort: true, title: '更新时间'}
            ]],
            error: function(code,msg){
                http.fail(code,msg);
            }
        });

        var fnSearch = function(){
            var param = fnGetSearchParam();
            bedTableIns.reload({where:param,page: {curr: 1}});
        };

        // 搜索按钮点击事件
        $view.find(".btnSearch").click(function () {
            fnSearch();
        });

        // 添加按钮点击事件
        $view.find(".btnAdd").click(function () {
            bedTableIns.addRow({status:1});
        });


        // 工具条点击事件
        table.on('tool(bedTable)', function (rowObj) {
            var data = rowObj.data;
            if (rowObj.event === 'edit') { //修改
            } else if (rowObj.event === 'del') { //删除
                layer.confirm("确定删除该行?",{btn:["确定","取消"]},
                    function(index,obj) {
                        layer.closeAll('dialog');
                        rowObj.del();
                    }
                );
            }
        });

        // 修改状态
        // form.on('switch(tpl-bedTable-status)', function (obj) {
        //     var idx = obj.othis.parents("tr:eq(0)").attr("data-index");
        //     var data = {id: obj.elem.value, status: obj.elem.checked ? 1 : 0};
        //     bedTableIns.saveDelta(idx,data);
        // });

        table.on('edit(bedTable)',function (row) {
            var idx = row.tr.attr("data-index");
            var data = {};
            data.id = row.data.id;
            data[row.field] = row.value;
            bedTableIns.saveDelta(idx,data);
        });

        form.on('select(bedSearch-deptCode)',function(data){
            fnSearch();
        });
        table.on("sort(bedTable)",function(obj){
            orderByClause = (obj.field || '')+ ' ' + (obj.type || '');
            fnSearch();
        });
        $view.find(".btnSave").click(function () {
            var data = bedTableIns.getDelta();
            if(!data){return;}
            http.post('bed/saveBatch',JSON.stringify(data),function(res){
                if(res.ok){
                    bedTableIns.updateCache(res.data);
                    bedTableIns.resetDelta();
                    http.okMsg(res.msg);
                }
            });
        });

    });

</script>